package com.jiangjiang.eshop.schedule.stock;

import com.jiangjiang.eshop.common.bean.SpringApplicationContext;
import com.jiangjiang.eshop.schedule.constant.StockUpdateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName ScheduleStockUpdaterFactory
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/31 7:27
 * @Version 1.0
 **/
@Component
public class ScheduleStockUpdaterFactory {
    @Autowired
    private SpringApplicationContext context;

    /**
     * 创建一个库存更新命令
     * @param stockUpdateEvent 参数对象
     * @param parameter 库存参数
     * @return
     */
    public ScheduleStockUpdater create(Integer stockUpdateEvent, Object parameter) {
        ScheduleStockUpdater stockUpdater = null;

        if(StockUpdateEvent.SUBMIT_ORDER.equals(stockUpdateEvent)) {
            stockUpdater = context.getBean(SubmitOrderScheduleStockUpdater.class);
        } else if(StockUpdateEvent.CANCEL_ORDER.equals(stockUpdateEvent)) {
            stockUpdater = context.getBean(CancelOrderScheduleStockUpdater.class);
        } else if(StockUpdateEvent.PAY_ORDER.equals(stockUpdateEvent)) {
            stockUpdater = context.getBean(PayOrderScheduleStockUpdater.class);
        } else if(StockUpdateEvent.PURCHASE_INPUT.equals(stockUpdateEvent)) {
            stockUpdater = context.getBean(PurchaseInputScheduleStockUpdater.class);
        } else if(StockUpdateEvent.RETURN_GOODS_INPUT.equals(stockUpdateEvent)) {
            stockUpdater = context.getBean(ReturnGoodsInputScheduleStockUpdater.class);
        }

        stockUpdater.setParameter(parameter);

        return stockUpdater;
    }
}
